<?
class iMVC_Validator{
    protected $rules=array();
    protected $errorList=array();
    private $request=array();

    function __construct($request){

        $this->request=$request;
    }
    //validate函数<<<
    public function validate(){
        if(count($this->request)>0){  
            foreach($this->request as $key=>$value){
                if($this->rules[$key]!=null){
                    $rs=$this->rules[$key][0];  
                    $msg=$this->rules[$key][1];  
                    $rs=json_decode($rs,true);
                    $msg=json_decode($msg,true);  
                    foreach($rs as $r=>$v){
                        //验证循环<<<
                        if($r=='requried'){
                            if(iMVC_Validator::isNull($value)){
                                $this->addError($msg,$r);
                            } 
                        }
                        else if($r=='max-length'){
                            if(iMVC_Validator::maxLength($value,$v)){
                                $this->addError($msg,$r);
                            }
                        }
                        else if($r=='min-length'){
                            if(iMVC_Validator::minLength($value,$v)){
                                $this->addError($msg,$r);
                            }
                        }
                        else if($r=='regRule'){
                            if(iMVC_Validator::regRule($v,$value)){
                                $this->addError($msg,$r);
                            } 
                        }
                        else if($r=='email'){
                            if(iMVC_Validator::email($value)){
                                $this->addError($msg,$r);
                            } 
                        }
                        else if($r=='equalTo'){
                            if(iMVC_Validator::equalTo($value,$v)){
                                $this->addError($msg,$r);
                            } 
                        }
                        //>>>

                    }
                } 
            }
            
        }
        return count($this->errorList);
    }
    //>>>
    
    /*addRule函数<<<
     @addRule($key,'{1}','{2}')
     @key为所需验证的字段 
     @{1},为验证规则如'{"required":true,"equalTo":1}'
     @{2},为验证规则对应错误输出{"default":"发生错误","requried":"必填"},不设置的字段将使用default
     @注意:'{1}','{2}',需要用单引号包裹
     */
    public function addRule($key,$rule,$errMsg=null){
        if(!$this->rules[$key]){
            $this->rules[$key]=array($rule,$errMsg);
        }
        else{
            //$this->rules.=$rules[$key][0]; 
            //$this->rules[$key]=array($rule,$errMsg);
        }
    }
    //>>>
    //addError函数<<<
    protected function addError($msg,$key){
        if($msg[$key]==null){
            if($msg['default']==null){

                $this->errorList[]='error hanppend'; 
            }
            else{
                $this->errorList[]=$msg['default']; 
            }
        }
        else{
            $this->errorList[]=$msg[$key];

        }

    }//>>>
    //getErrorList函数<<<
    public function getErrorList(){
        return $this->errorList; 
    
    }//>>>
    //一系列静态验证函数<<<
    //非空验证,非空为true
    public static function isNull($value){

        if(strlen($value)>0){

            return false;  
        } 
        else{
            return true;  
        }
    } 
    //最大值,大于最大值为true
    public static function maxLength($value,$leng){

        if(strlen($value)<=$leng){

            return false;  
        } 
        else{
            return true;  
        }
    }
    //最小值,小于最小值为true
    public static function minLength($value,$leng){

        if(strlen($value)>=$leng){

            return false;  
        } 
        else{
            return true;  
        }
    }
    //正则不匹配为true
    public static function regRule($partten,$value){
        if(preg_match($partten,$value)){
            return false; 
        }
        else{
            return true; 
        }
    } 
    //email不匹配true
    public static function email($value){
        $partten="/[a-zA-Z1-9._-]+@[a-zA-Z1-9._-]+\.[a-zA-Z1-9._-]+/";
        return iMVC_Validator::regRule($partten,$value); 

    }
    public static function equalTo($value1,$value2){

        if($value1==$value2){

            return false; 
        } 
        else{
            return true; 
        }

    }
    //>>>


}
